IPM-Buy/Sell-Pressure-Main


declare lower;

input length = 14;
input smoothLength = 5;
input volLookback = 20;
input atrLength = 14;
input momentumLength = 3;
input pressureScale = 100;
input priceLookback = 5;
input colorBars = yes;
input showLabels = yes;
input useEMA = yes;
input biasLevel = -45;

#--------------------------------------------------
# 1. BAR STRUCTURE / ORDER FLOW PROXY
#--------------------------------------------------
def barRange = high - low;
def safeRange = if barRange == 0 then 0.01 else barRange;
def closePos = (close - low) / safeRange;

def rawBuyVol = volume * closePos;
def rawSellVol = volume * (1 - closePos);

def buyBase =
    if useEMA then ExpAverage(rawBuyVol, length)
    else Average(rawBuyVol, length);

def sellBase =
    if useEMA then ExpAverage(rawSellVol, length)
    else Average(rawSellVol, length);

def buyFlow =
    if useEMA then ExpAverage(buyBase, smoothLength)
    else Average(buyBase, smoothLength);

def sellFlow =
    if useEMA then ExpAverage(sellBase, smoothLength)
    else Average(sellBase, smoothLength);

def flowDelta = buyFlow - sellFlow;

def avgVol = Average(volume, volLookback);
def safeAvgVol = if avgVol == 0 then 1 else avgVol;
def flowComponent = flowDelta / safeAvgVol;

#--------------------------------------------------
# 2. RANGE EXPANSION
#--------------------------------------------------
def tr = TrueRange(high, close, low);
def atr = Average(tr, atrLength);
def safeATR = if atr == 0 then 0.01 else atr;

def rangeExpansion = (barRange / safeATR) - 1;

def dirRange =
    if close > open then rangeExpansion
    else if close < open then -rangeExpansion
    else 0;

#--------------------------------------------------
# 3. VOLUME EXPANSION
#--------------------------------------------------
def volExpansion = (volume / safeAvgVol) - 1;

def dirVol =
    if close > open then volExpansion
    else if close < open then -volExpansion
    else 0;

#--------------------------------------------------
# 4. MOMENTUM ACCELERATION
#--------------------------------------------------
def momentumRaw = close - close[momentumLength];
def momentumNorm = momentumRaw / safeATR;
def momentumAccel = momentumNorm - momentumNorm[1];

#--------------------------------------------------
# 5. LIQUIDITY PRESSURE ENGINE
#--------------------------------------------------
def rawPressure =
      (flowComponent * 0.45)
    + (dirRange * 0.20)
    + (dirVol * 0.20)
    + (momentumAccel * 0.15);

def liquidityPressure =
    if useEMA then ExpAverage(rawPressure * pressureScale, smoothLength)
    else Average(rawPressure * pressureScale, smoothLength);

def signalLine =
    if useEMA then ExpAverage(liquidityPressure, smoothLength)
    else Average(liquidityPressure, smoothLength);

#--------------------------------------------------
# 6. PRICE TREND VS PRESSURE (FOR CANDLE COLORING)
#--------------------------------------------------
def priceRising = close > close[priceLookback];
def priceFalling = close < close[priceLookback];

def bearishWarning = priceRising and liquidityPressure < 0;
def bullishWarning = priceFalling and liquidityPressure > 0;

rec pressureState =
    if liquidityPressure > 0 then 1
    else if liquidityPressure < 0 then -1
    else pressureState[1];

#--------------------------------------------------
# 7. FAST LIQUIDITY LINE (THINNER)
#--------------------------------------------------
plot FastLiquidity = liquidityPressure;
FastLiquidity.SetPaintingStrategy(PaintingStrategy.LINE);
FastLiquidity.SetLineWeight(1);
FastLiquidity.AssignValueColor(
    if liquidityPressure > signalLine then Color.CYAN
    else Color.MAGENTA
);

#--------------------------------------------------
# 8. MIDLINE
#--------------------------------------------------
plot MidLine = 0;
MidLine.SetDefaultColor(Color.GRAY);
MidLine.SetStyle(Curve.SHORT_DASH);

#--------------------------------------------------
# 9. BIAS LINE BELOW INDICATOR
#--------------------------------------------------
plot BiasLine = biasLevel;
BiasLine.SetLineWeight(3);
BiasLine.AssignValueColor(
    if signalLine > 0 then Color.GREEN
    else Color.RED
);

#--------------------------------------------------
# 10. CANDLE COLORING
#--------------------------------------------------
AssignPriceColor(
    if !colorBars then Color.CURRENT
    else if bearishWarning then Color.MAGENTA
    else if bullishWarning then Color.CYAN
    else if pressureState == 1 then Color.GREEN
    else if pressureState == -1 then Color.RED
    else Color.CURRENT
);

#--------------------------------------------------
# 11. LABELS
#--------------------------------------------------
AddLabel(showLabels,
    "Liquidity Fast: " + Round(liquidityPressure, 2),
    if liquidityPressure > signalLine then Color.CYAN else Color.MAGENTA
);

AddLabel(showLabels,
    "Liquidity Slow: " + Round(signalLine, 2),
    if signalLine > 0 then Color.GREEN else Color.RED
);

AddLabel(showLabels,
    if bearishWarning then "PRICE UP / SELLING"
    else if bullishWarning then "PRICE DOWN / BUYING"
    else if pressureState == 1 then "BUYING"
    else if pressureState == -1 then "SELLING"
    else "NEUTRAL",
    if bearishWarning then Color.MAGENTA
    else if bullishWarning then Color.CYAN
    else if pressureState == 1 then Color.GREEN
    else if pressureState == -1 then Color.RED
    else Color.GRAY
);

AddLabel(showLabels,
    if signalLine > 0 then "BULLISH"
    else if signalLine < 0 then "BEARISH"
    else "NEUTRAL",
    if signalLine > 0 then Color.GREEN
    else if signalLine < 0 then Color.RED
    else Color.GRAY
);
